<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>添加客户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <div class="layui-container">
        <div class="layui-row">
            <div class="layui-col-lg-offset3 layui-col-lg6">
                <div class="layui-form layuimini-form">

                    <div class="layui-form-item">
                        <label class="layui-form-label required">照片：</label>
                        <input type="hidden" name="gpic" value="" id="gpic">
                        <div class="layui-input-block">
                            <div class="layui-upload">
                                <button type="button" class="layui-btn" id="test1">上传图片</button>
                                <div class="layui-upload-list">
                                    <img style="width: 100px;" class="layui-upload-img" id="demo1">
                                    <p id="demoText"></p>
                                </div>
                                <div style="width: 95px;">
                                    <div class="layui-progress layui-progress-big" lay-showpercent="yes"
                                        lay-filter="demo">
                                        <div class="layui-progress-bar" lay-percent=""></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">客户姓名：</label>
                        <div class="layui-input-block">
                            <input type="text" name="gname" lay-verify="required" lay-reqtext="姓名不能为空"
                                placeholder="请输入客户姓名" class="layui-input">

                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label required">客户性别：</label>
                        <div class="layui-input-block">
                            <input type="radio" name="ggender" value="男" title="男" checked="">
                            <input type="radio" name="ggender" value="女" title="女">
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label required">客户电话：</label>
                        <div class="layui-input-block">
                            <input type="text" name="gphone" lay-verify="required" lay-reqtext="电话不能为空"
                                placeholder="请输入客户电话" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">客户余额：</label>
                        <div class="layui-input-block">
                            <input type="text" name="gmoney" lay-verify="required" lay-reqtext="余额不能为空"
                                placeholder="请输入客户余额" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">添加客户</button>
                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>
    <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'upload', 'element'], function () {
            var form = layui.form,
                layer = layui.layer,
                upload = layui.upload,
                element = layui.element,
                $ = layui.$;


            var uploadInst = upload.render({
                elem: '#test1'
                , url: 'http://localhost:8080/guest/upload'
                , before: function (obj) {
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result);
                    });

                    element.progress('demo', '0%');
                    layer.msg('上传中...', { icon: 16, time: 0 })
                },
                done: function (res) {
                    if (res.code == 400) {
                        return layer.msg('上传失败');
                    }

                    if (res.code == 200) {
                        $('#demo1').attr('src', res.result);
                        $('gpic').val(res.result);
                    }

                    $('#demoText').html('');
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                },
                progress: function (n, elem, e) {
                    element.progress('demo', n + '%');

                    if (n == 100) {
                        layer.msg('上传完毕', { icon: 1 })
                    }
                }
            })

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                $.ajax({
                    "url": "http://localhost:8080/guest/insert",
                    "data": data.field,
                    "success": function (result) {
                        if (result.code == 200) {
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        } else {
                            layer.msg(result.msg);
                        }
                    }
                });

                return false;
            });

        });
    </script>
</body>

</html>